package com.swpu.cbg.cnshop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.bean.HomeCategory;
import com.swpu.cbg.cnshop.bean.HomeContentCategory;
import com.swpu.cbg.cnshop.bean.HomeContentItemCategory;

import java.util.List;

/**
 * Created by chenboge on 16/2/1.
 */
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {

    //判断子view使用哪一个（cardview或cardview1）
    private final int RECYCLEVIEW_CHILDVIEW_LEFT = 0;
    private final int RECYCLEVIEW_CHILDVIEW_RIGHT = 1;

    private Context mContext;
    private List<HomeContentCategory> mDatas;
    private onItemClickListener onItemClickListener;

    public HomeAdapter(Context mContext, List<HomeContentCategory> homeContentCategories) {
        this.mContext = mContext;
        this.mDatas = homeContentCategories;
    }

    public void setOnItemClickListener(onItemClickListener itemClickListener){
        this.onItemClickListener=itemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = null;
        if (viewType == RECYCLEVIEW_CHILDVIEW_LEFT) {

            mView = LayoutInflater.from(mContext).inflate(R.layout.template_home_cardview, parent, false);
        } else {

            mView = LayoutInflater.from(mContext).inflate(R.layout.template_home_cardview2, parent, false);
        }
        ViewHolder mHOlder = new ViewHolder(mView);
        return mHOlder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeContentCategory itemCategory = mDatas.get(position);
        holder.tv_title.setText(itemCategory.getTitle());

        Picasso.with(mContext).load(itemCategory.getCpOne().getImgUrl()).into(holder.iv_bigimg);
        Picasso.with(mContext).load(itemCategory.getCpTwo().getImgUrl()).into(holder.iv_smallimg_top);
        Picasso.with(mContext).load(itemCategory.getCpThree().getImgUrl()).into(holder.iv_smallimg_buttom);

//        holder.iv_bigimg.setImageResource(mDatas.get(position).getImgBig());
//        holder.iv_smallimg_top.setImageResource(mDatas.get(position).getImgSmallTop());
//        holder.iv_smallimg_buttom.setImageResource(mDatas.get(position).getImgSmallBottom());
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return RECYCLEVIEW_CHILDVIEW_LEFT;
        } else {
            return RECYCLEVIEW_CHILDVIEW_RIGHT;
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView tv_title = null;
        ImageView iv_bigimg;
        ImageView iv_smallimg_top;
        ImageView iv_smallimg_buttom;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.text_title);
            iv_bigimg = (ImageView) itemView.findViewById(R.id.imgview_big);
            iv_smallimg_top = (ImageView) itemView.findViewById(R.id.imgview_small_top);
            iv_smallimg_buttom = (ImageView) itemView.findViewById(R.id.imgview_small_bottom);
            iv_bigimg.setOnClickListener(this);
            iv_smallimg_top.setOnClickListener(this);
            iv_smallimg_buttom.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            HomeContentCategory category = mDatas.get(getLayoutPosition());

            if (onItemClickListener != null) {
                switch (v.getId()) {
                    case R.id.imgview_big:
                        onItemClickListener.onClick(v,category);
                        break;
                }
            }
        }
    }


    public interface onItemClickListener{

        void onClick(View view,HomeContentCategory category);
    }
}
